//静态资源管理设置文件  ，使得static目录下的文件可以在服务器中访问
var http = require("http");
var url = require("url");
var path = require("path");
var fs = require("fs");
http.createServer(function(req, res){
	if(req.url == '/favicon.ico'){
		return ;
	}
	var pathname = url.parse(req.url).pathname;
	pathname = pathname == '/' ? 'index.html' : pathname;
	var extname = path.extname(pathname);
	fs.readFile('./static/'+pathname, function(err, data){
		if(err){
			fs.readFile('./static/404.html', function(err, data){
				res.writeHead(404,{"content-type":"text/html;charset=utf8"});
			    res.end(data);
			});			
		}else{
			getMime(extname, function(mime) {
				res.writeHead(200,{"content-type":mime});
			    res.end(data);
			});
			
		}
	});	
}).listen(3000,"127.0.0.1");

function getMime(extname, callback){
	fs.readFile('./static/mime.json', function(err, data) {
		var mimeJson = JSON.parse(data);
		callback(mimeJson[extname] || "text/plain");
	});
}
